package com.tobe;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private EditText contentEditText;
	private Button switchButton;
	private Button button0, button1, button2;
	private Button button3, button4, button5;
	private Button button6, button7;
	private Button randomButton;
	private Button comfirmButton;
	
	private boolean isConnect = false;
	private String ip = "10.235.16.202";
	private Socket socket;
	private PrintWriter output;
	private String content;
	private String alphabet = "abcdefghijklmnopqrstuvwxyz";


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		contentEditText = (EditText)findViewById(R.id.content);
		switchButton = (Button)findViewById(R.id.switchButton);
		button0 = (Button)findViewById(R.id.button0);
		button1 = (Button)findViewById(R.id.button1);
		button2 = (Button)findViewById(R.id.button2);
		button3 = (Button)findViewById(R.id.button3);
		button4 = (Button)findViewById(R.id.button4);
		button5 = (Button)findViewById(R.id.button5);
		button6 = (Button)findViewById(R.id.button6);
		button7 = (Button)findViewById(R.id.button7);
		randomButton = (Button)findViewById(R.id.random);
		comfirmButton = (Button)findViewById(R.id.comfirm);
		
		switchButton.setOnClickListener(this);
		button0.setOnClickListener(this);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        randomButton.setOnClickListener(this);
        comfirmButton.setOnClickListener(this);	
        comfirmButton.setClickable(false);
	}

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch(id){
            case R.id.switchButton:
                if(isConnect){
                    output.println('*');
                    isConnect = false; // 如果已经连上则关闭Socket
                    v.setBackgroundResource(R.drawable.switch_off);    
                    comfirmButton.setClickable(false);
                    try {
                        output.close();
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else{
                    isConnect = true; // 如果没有连上则重启Socket
                    v.setBackgroundResource(R.drawable.switch_on);
                    try {
                        socket = new Socket(ip, 4343);
                        output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);      
                        comfirmButton.setClickable(true);
                    } catch (IOException e) {
                        isConnect = false; // 如果连不上有exception则不认为连上，否则后面强退
                        v.setBackgroundResource(R.drawable.switch_off);
                        Toast.makeText(MainActivity.this, "未连上服务器，请重试...", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }
                }               
                break;
            case R.id.button0:
            case R.id.button1:
            case R.id.button2:
            case R.id.button3:
            case R.id.button4:
            case R.id.button5:
            case R.id.button6:
            case R.id.button7:
                if(isConnect){
                    content = ((Button)v).getText().toString().trim();
                    contentEditText.setText(content);
                    output.println(content);
                }
                break;
            case R.id.random:
                content = "";
                Random random = new Random();
                for(int i=0; i<4; ++i){
                    content += alphabet.charAt(Math.abs(random.nextInt()%26));
                }
                contentEditText.setText(content);
                break;
            case R.id.comfirm:
                if(isConnect && content!=""){
                    contentEditText.setText("");
                    output.println(content);
                }
                break;
        }
        
    }


    @Override
    public void onBackPressed() {

        new AlertDialog.Builder(MainActivity.this)
        .setTitle("提醒") 
        .setIcon(R.drawable.reomte_library_controller)
        .setMessage("你确定要退出程序?")
        .setPositiveButton("退出",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {                 
                        System.exit(0);
                    }
                })
        .setNeutralButton("设置IP", 
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, 
                    int which) {
                final EditText ipEditText = new EditText(MainActivity.this); 
                new AlertDialog.Builder(MainActivity.this)
                    .setTitle("请输入IP地址")
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setView(ipEditText)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            ip = ipEditText.getText().toString();
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
            }
        })
        .setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {
                        //do nothing
                    }
                }).show();  
    }
    
    
}
